import Http from '../../utils/http.js';

let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    chooseSize: true,
    animationData: {},
    loading: false,
    list: [],//列表数据
    curName: '',//列表数据第一项
  },

  //点击列表商品
  itemClick(e) {
    const commodityCode = e.detail.commodityCode;
    wx.navigateTo({
      url: `./carDetail/carDetail?commodityCode=${commodityCode}`
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCarList()
  },
  getCarList() {
    this.setData({
      loading: true
    })
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    Http.Request('GET', `/api/customer/commodity/getCarPage/v1`).then(res => {
      this.setData({
        loading: false
      })
      wx.hideLoading();
      this.formatList(res)
    }).catch(err => {
      wx.hideLoading();
      app.globalFn.reLoad(this.getHouseList, err)
    })
  },
  formatList(arr) {
    let list = [];
    let obj = {};
    arr.forEach((o, i) => {
      obj[i] = [{ name: o.groupName }];
      if (o.list && o.list.length) {
        obj[i] = obj[i].concat(o.list)
      }
    })
    list.push(obj);
    const curName = list[0][0][0].name;
    this.setData({
      list,
      curName
    })
  },
  onShareAppMessage() {
    app.globalFn.onShareAppMessage()
  }
})